package peng.yang.express.Util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import DataBaseUtils.SQLLiteHelperCheck;
import DataBaseUtils.SQLLiteHelperPhone;
import peng.yang.express.Bean.ExpressMember;
import peng.yang.express.Bean.PhoneMembers;
import peng.yang.express.Bean.PhoneRequest;

/**
 * Created by Administrator on 2016/7/1.
 */
public class HistoryMembersDao {

    SQLLiteHelperCheck openHelper;
    SQLiteDatabase database;

    //对expressinfo进行增删改查

    /*
    * 借助构造方法获取上下文对象()context
    * */

    public HistoryMembersDao(Context context) {
        openHelper = new SQLLiteHelperCheck(context);
        database = openHelper.getWritableDatabase();
    }

    //插入一条记录
    public long insert(ExpressMember member) {
        SQLiteDatabase database = openHelper.getReadableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("num",member.getNum());
        cv.put("expressName", member.getExpName());
        cv.put("goodName", member.getGoodname());
        cv.put("fileImageUrl", member.getFileImageUrl());
        cv.put("state", member.getState());
        long row = database.insert("expressCheck", null, cv);
        return row;
    }

    //    获得数据库所有的数据
    public List<ExpressMember> getAllMembers() {
        List<ExpressMember> memberList = new ArrayList<>();
        SQLiteDatabase database = openHelper.getReadableDatabase();
//        要查询的字段
        String[] columns = {"num", "expressName", "goodName", "fileImageUrl","state"};
//        调用query查询
        Cursor cursor = database.query("expressCheck", columns, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String num = cursor.getString(cursor.getColumnIndex("num"));
            String expressName = cursor.getString(cursor.getColumnIndex("expressName"));
            String goodName = cursor.getString(cursor.getColumnIndex("goodName"));
            String fileImageUrl = cursor.getString(cursor.getColumnIndex("fileImageUrl"));
            String state = cursor.getString(cursor.getColumnIndex("state"));

            ExpressMember member = new ExpressMember();
            member.setNum(num);
            member.setExpName(expressName);
            member.setFileImageUrl(fileImageUrl);
            member.setGoodname(goodName);
            member.setState(state);
            memberList.add(member);
        }
        return memberList;
    }

    /**
     * 根据快递单号删除快递信息
    * @param num
    */

    public void delete(String num){
        String  where = "num = ?";
        database.delete("expressCheck",where,new String[]{num});
    }


    public  void update(String num, List<PhoneRequest> sourcelist){
        SQLiteDatabase database = openHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("state", sourcelist.get(0).getState().toString()+sourcelist.get(0).getTime().toString());
        String  where = " num =?";
        database.update("expressCheck",values,where,new String[]{num});
    }

    //插入数据库
    /*
    * 是对数据库中的数据进行操作，那么首先要去获得SQLiteDatabase
    * 该类是通过SQLiteOpenHelperd的对象，调用getReadable...getWriteale....
    * */


    /*public void insert(String expressno,String companyname){

      *//*  String sql = "insert into expressinfo(expressno,companyname) values('"+expressno+"','"+companyname+"')";
        database.execSQL(sql);*//*
        ContentValues values = new ContentValues();
        values.put("expressno",expressno);
        values.put("companyname",companyname);
        database.insert("expressinfo",null,values);

    }

    public void insertData(ExpressBean expressBean){
        ContentValues values = new ContentValues();
        values.put("expressno",expressBean.getExpressNo());
        values.put("companyname",expressBean.getCompanyName());
        database.insert("expressinfo",null,values);


    }


    *//**
     * 查询单条数据
     * @param expressno
     * @return
     *//*

    public ExpressBean queryExpress(String expressno){
        *//*
        ？占位符
        * *//*
        String sql = "select * from expressinfo where expressno=?";
        Cursor cursor= database.rawQuery(sql,new String[]{expressno});
        if (cursor.moveToNext()){
            // String companyname  = cursor.getString(1);
            String companyname  = cursor.getString(cursor.getColumnIndex("companyname"));
            ExpressBean bean = new ExpressBean(expressno,companyname);
            return  bean;
        }


        return null;
    }




    *//**
     * 查询多条数据
     * @return
     *//*

    public List<ExpressBean> queryAllExpressBean(){
        String sql = "select * from expressinfo";
        Cursor cursor= database.rawQuery(sql,null);
        List<ExpressBean> beanList = new ArrayList<>();
        while (cursor.moveToNext()){
            ExpressBean bean = new ExpressBean();
            String name = cursor.getString(cursor.getColumnIndex("companyname"));
            bean.setCompanyName(name);
            beanList.add(bean);
        }

        return  beanList;

    }

    *//**
     * 根据快递单号删除快递信息
     * @param expressNo
     *//*

    public void delete(String expressNo){
        *//*String  sql  = "delete from expressinfo where expressno = "+expressNo;

        database.execSQL(sql);*//*
        String  where = "expressno = ?";
        database.delete("expressinfo",where,new String[]{expressNo});
    }

    public  void update(String expressNo){

       *//* String  sql = "update expressinfo set companyname = '申通' where expressno = '"+ expressNo+"'";

        database.execSQL(sql);*//*
        ContentValues values = new ContentValues();
        *//*
        companyname是表的字段的名字
        "韵达"是companyname所对应的新值
        * *//*
        values.put("companyname","韵达");
        *//*
        where指定条件
        new String[]{expressNo}：提供条件满足的值
        * *//*
        String  where = " expressno =?";
        database.update("expressinfo",values,where,new String[]{expressNo});


    }

    public void updateData(String expressNo,ContentValues values){
        String  where = " expressno =?";
        database.update("expressinfo",values,where,new String[]{expressNo});


    }*/
}
